//
//  HostLoginController.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/4/22.
//

import UIKit
import SnapKit

class HostLoginController: ZsBaseViewController {
    
    var countryCodeModel:WLCountryCode?

    override func viewDidLoad() {
        
        super.viewDidLoad()
        self.setUI()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super .viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: true)
        if countryCodeModel?.phonecode.count ?? 0 <= 0 {
            self.configurationInfo()
        }
    }
   
    override func viewWillDisappear(_ animated: Bool) {
        super .viewWillDisappear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        self.view.endEditing(true)
    }
    
    func setUI() {
        self.view.addSubview(self.homeClickCountryView)
        self.view.addSubview(self.logoBtn)
        self.view.addSubview(self.importPhoneView)
        self.view.addSubview(self.loginWayView)
        self.view.addSubview(self.loginBottonView)
        self.view.addSubview(self.loginQueryLogisticsV)
        
        self.homeClickCountryView.snp.makeConstraints { (maker) in
            maker.top.equalTo(self.view.snp.topMargin).offset(0)
            maker.left.equalToSuperview().offset(25)
            maker.height.equalTo(homeClickCountryView.heightInterval)
            maker.width.equalTo(homeClickCountryView.widthInterval * 3)
        }
        self.logoBtn.snp.makeConstraints { (maker) in
            maker.top.equalTo(self.view.snp.topMargin).offset(46)
            maker.height.equalTo(100)
            maker.width.equalTo(100)
            maker.centerX.equalTo(self.view.snp.centerX)
        }
        self.importPhoneView.snp.makeConstraints { (maker) in
            maker.top.equalTo(self.logoBtn.snp.bottom).offset(10)
            maker.height.equalTo(45)
            maker.left.equalToSuperview().offset(25)
            maker.right.equalToSuperview().offset(-25)
        }
        self.loginWayView.snp.makeConstraints { (maker) in
            maker.top.equalTo(self.importPhoneView.snp.bottom).offset(40)
            maker.height.equalTo(130)
            maker.left.equalToSuperview().offset(25)
            maker.right.equalToSuperview().offset(-25)
        }
        self.loginQueryLogisticsV.snp.makeConstraints { (maker) in
            maker.top.equalTo(self.loginWayView.snp.bottom).offset(35)
            maker.height.equalTo(85)
            maker.left.equalToSuperview().offset(25)
            maker.right.equalToSuperview().offset(-25)
        }
        self.loginBottonView.snp.makeConstraints { (maker) in
            maker.bottom.equalTo(self.view.snp.bottomMargin).offset(-30)
            maker.height.equalTo(70)
            maker.left.equalToSuperview().offset(25)
            maker.right.equalToSuperview().offset(-25)
        }
    }
    
    func refreshUI(index:Int) {
        self.loginWayView.refreshUI(index: index)
        self.loginQueryLogisticsV.refreshUI(index: index)
        self.loginBottonView.refreshUI(index: index)
        self.importPhoneView.refreshUI(index: index)

        //缅甸
        if index == 101 {
            
        //英文
        } else if index == 102 {
        
        //中文
        } else if index == 103 {
            
        }
             
    }
    
    func getConfigurationInfo(index:Int) {
       
    }
    
    func configurationInfo() {
        let userInfoDefaults = UserDefaults.standard
        let week:NSInteger = NSCalendar.current.ordinality(of: .weekday, in: .year, for: NSDate.init() as Date)!
        var weekValue:NSInteger = userInfoDefaults.integer(forKey: WLUserModel.getPhone() + ActionHeader.WLSmsCountry)
        if weekValue != week {
            HttpService.sharedHttpService().getSmsCountrySuccess { (state, countryCode) in
                if (state) {
                    UserDefaults.standard.setValue(week, forKey: WLUserModel.getPhone()+ActionHeader.WLSmsCountry)
                    self.countryCodeModel = countryCode
                    self.importPhoneView.selectCountryCodeBrn.setTitle(self.countryCodeModel?.phonecode, for: .normal)
                }
            }
        } else {
            let model:Array<WLCountryCode> = CacheTool.cacheTool.searchCountryCodes()
            if (model.count > 1) {
                self.countryCodeModel = model.first
                self.importPhoneView.selectCountryCodeBrn.setTitle(self.countryCodeModel?.phonecode, for: .normal)
            }
        }
        weekValue = UserDefaults.standard.integer(forKey: WLUserModel.getPhone() + ActionHeader.WLMyanmarArea)
        if (weekValue == week ) {
            HttpService.sharedHttpService().getMyanmarAreaSuccess { (state) in
                if state {
                    UserDefaults.standard.setValue(week, forKey: WLUserModel.getPhone() + ActionHeader.WLMyanmarArea)
                }
            }
        }
    }
    
    
    lazy var homeClickCountryView:HomeClickCountryView = {
        let homeClickCountryView = HomeClickCountryView()
        homeClickCountryView.clickCountryBlock = { indexNum in
            self.refreshUI(index: indexNum)
            self.getConfigurationInfo(index: indexNum)
        }
        return homeClickCountryView
    }()
    
    lazy var logoBtn:UIButton = {
        let logoBtn = UIButton()
        logoBtn.setImage(UIImage.init(named: "login_Logo"), for: .normal)
        return logoBtn
    }()
    
    lazy var contactUSView:ContactUSView = {
        let contactUSView = ContactUSView.init(frame: CGRect.init(x: 0, y: 0, width: Constants.screenWidth, height: Constants.screenHeight))
        
        return ContactUSView()
    }()
    
    lazy var selectCountryView:SelectCountryView = {
        let selectCountryView = SelectCountryView.init(frame: CGRect.init(x: 0, y: 0, width: Constants.screenWidth, height: Constants.screenHeight))
        selectCountryView.selectCountryViewBlock = { countryCode in
            self.countryCodeModel = countryCode
            self.importPhoneView.selectCountryCodeBrn.setTitle(self.countryCodeModel?.phonecode, for: .normal)
        }
        selectCountryView.refreshData()
        return selectCountryView
    }()
    
    lazy var importPhoneView:ImportPhoneView = {
        let importPhoneView = ImportPhoneView()
        importPhoneView.importPhoneBlock = {
            if (self.countryCodeModel != nil) {
                Constants.window?.addSubview(self.selectCountryView)
            } else {
                self.configurationInfo()
            }
        }
        return importPhoneView
    }()

    lazy var loginWayView:LoginWayView = {
        let loginWayView = LoginWayView()
        loginWayView.loginWayViewBlock = { loginWay in
            if self.loginBottonView.isSelect != true {
                Constants.window?.makeToast(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.hint_agreeAgreement))
                return
            }
            if loginWay == 1 {
                let passWordLogin:PassWordLoginController = PassWordLoginController()
                self.navigationController?.pushViewController(passWordLogin, animated: true)
            } else if loginWay == 2 {
                
            } else if loginWay == 3 {
                
            } else if loginWay == 4 {
                if (self.countryCodeModel?.phonecode.count ?? 0 <= 0) {
                    self.configurationInfo()
                    return
                }
            }
        }
        return loginWayView
    }()
    
    lazy var loginQueryLogisticsV:LoginQueryLogisticsV = {
        let loginQueryLogisticsV = LoginQueryLogisticsV()
        loginQueryLogisticsV.queryLogisticsBlock = {
            
        }
        return loginQueryLogisticsV
    }()
    
    lazy var loginBottonView:LoginBottonView = {
        let loginBottonView = LoginBottonView();
        loginBottonView.loginBottonViewBlock = { type in
            if type == LoginBottonType.clickCountUs {
                Constants.window?.makeToast("")
                HttpService.sharedHttpService().getPhoneListSuccess { (resultData, state, message) in
                    if state {
                        Constants.window?.addSubview(self.contactUSView)
                        self.contactUSView.setDataSource(dataSource: resultData.array ?? [])
                    } else {
                        Constants.window?.makeToast(message)
                    }
                }

            } else if type == LoginBottonType.clickAgreement {
                let agreementC:AgreementController = AgreementController()
                self.navigationController?.pushViewController(agreementC, animated: true)
            } else if type == LoginBottonType.clickSelectImg {
                
            }
        }
        return loginBottonView
    }()
    
}
